﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using Interactive.Utility;
using System.Configuration;

namespace TravelLink.DAL {
    public class DataContextFactory {

        private static readonly string Default_Connection_Name = "DataConnection";

        private static Dictionary<string, string> connection;

        static DataContextFactory() {
            connection = new Dictionary<string, string>(5);

            foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings) {
                connection[item.Name] = item.ConnectionString;
            }
        }

        public static TravelLinkDBDataContext Create() {

            TravelLinkDBDataContext ctx = new TravelLinkDBDataContext(GetConnection<TravelLinkDBDataContext>());
            ctx.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
            
            return ctx;
        }


        protected static string GetConnection<T>() {
            string value = string.Empty;
            if (!connection.TryGetValue(typeof(T).ToString(), out value)) {
                return connection[Default_Connection_Name];
            }
            return value;
        }
    }
}
